<?php
/**
 * Created by PhpStorm.
 * User: silenceper
 * Date: 2017/5/9
 * Time: 下午6:02
 */

namespace mybackend\controllers\ext;


use common\repositories\myhtsq\ContractManageRepository;
use common\repositories\myhtsq\ContractReceivableRepository;
use yii\helpers\ArrayHelper;

class ContractManageController extends BaseSignController
{
    /**
     * 根据合同code获取合同id
     * 查询是否有回款
     */
    public function actionGetContractIdAndHasReceivable()
    {
        $contractCodes = \Yii::$app->request->post('contract_code');

        if (empty($contractCodes)) {
            $this->exportJson([], '-1', '参数contract_code必填');
        }

        $contractCodes = trim($contractCodes);
        $contractCodes = explode(',', $contractCodes);
        $data = ContractManageRepository::getContractMangeIdsByCode($contractCodes);

        $manageIds = ArrayHelper::getColumn($data, 'contract_id');
        $receivableList = ContractReceivableRepository::getReceivableByContractId($manageIds);
        $receivableGroupBy = [];
        if ($receivableList) {
            foreach ($receivableList as $receivable) {
                $receivableGroupBy[$receivable['contract_id']] = 1;
            }
        }

        $result = [
            'manageId' => [],
            'hasReceivable' => [],
        ];
        foreach ($data as $item) {
            $contractId = $item['contract_id'];
            $contractCode = $item['contract_code'];

            $result['manageId'][$contractCode] = $contractId;
            $result['hasReceivable'][$contractId] = isset($receivableGroupBy[$contractId]) ? 1 : 0;
        }

        $this->exportJson($result);
    }
}